﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Application.App;
using Domain.Config;
using DTO;
using FacadeApi.Extension;
using Infrastructure.Error;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

namespace FacadeApi.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class UpLevelController : ControllerBase
    {
        public ILogger<UpLevelController> _logger { get; set; }

        public UserApplication userApplication { get; set; }

        public WalletApplication walletApplication { get; set; }

        /// <summary>
        /// 获取超级商人升级列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetUpLevelByTraderList()
        {
            try
            {
                int M1 = SystemConfig.GetConfigByTime<int>("系统配置", $"等级.超级商人.1个月", TimeSpan.FromDays(1));
                int M3 = SystemConfig.GetConfigByTime<int>("系统配置", $"等级.超级商人.3个月", TimeSpan.FromDays(1));
                int M6 = SystemConfig.GetConfigByTime<int>("系统配置", $"等级.超级商人.6个月", TimeSpan.FromDays(1));
                int M12 = SystemConfig.GetConfigByTime<int>("系统配置", $"等级.超级商人.12个月", TimeSpan.FromDays(1));

                return this.Success(new object[] { new { Name = "一个月", Fee = M1, Month = 1 }, new { Name = "三个月", Fee = M3, Month = 3 }, new { Name = "六个月", Fee = M6, Month = 6 }, new { Name = "十二个月", Fee = M12, Month = 12 } });
            }
            catch (BizException ex)
            {
                return this.FailResult(ex.data, ex.Message);
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
                return this.FailResult(null, "系统繁忙，请稍后重试!");
            }
        }


        /// <summary>
        /// 超级商人升级 
        /// </summary>
        /// <param name="upLevelByTrader">几个月 1,3,12 </param>
        /// <returns>UpLevelSuccess=false 需要冲值金额</returns>
        [HttpPost, TokenValidFilter]
        public ActionResult PayUpLevelByTrader(PayUpLevelByTrader upLevelByTrader)
        {
            try
            {
                var month = upLevelByTrader.Month;

                var uid = User.Claims.First(i => i.Type == "ID").Value;

                var r = userApplication.PayUpLevelByTrader(int.Parse(uid), month);
                if (r == 0)
                {
                    return this.Success(new { UpLevelSuccess = true, PayMoney = 0 }, "提交成功!");
                }
                return this.Success(new { UpLevelSuccess = false, PayMoney = r }, "提交成功!");
            }
            catch (BizException ex)
            {
                return this.FailResult(ex.data, ex.Message);
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
                return this.FailResult(null, "系统繁忙，请稍后重试!");
            }
        }

        /// <summary>
        /// 获取任务达人升级列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetUpLevelByUserList()
        {
            try
            {
                int M1 = SystemConfig.GetConfigByTime<int>("系统配置", $"等级.任务达人.1个月", TimeSpan.FromDays(1));


                return this.Success(new object[] { new { Name = "一个月", Fee = M1, Month = 1 }});
            }
            catch (BizException ex)
            {
                return this.FailResult(ex.data, ex.Message);
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
                return this.FailResult(null, "系统繁忙，请稍后重试!");
            }
        }

        /// <summary>
        /// 升级任务达人
        /// </summary>
        /// <param name="upLevelByTrader"></param>
        /// <returns></returns>
        [HttpPost, TokenValidFilter]
        public ActionResult PayUpLevelByUser(PayUpLevelByUser payUpLevelByUser)
        {
            try
            {
                var month = payUpLevelByUser.Month;

                var uid = User.Claims.First(i => i.Type == "ID").Value;

                var r = userApplication.PayUpLevelByUser(int.Parse(uid), month);
                if (r == 0)
                {
                    return this.Success(new { UpLevelSuccess = true, PayMoney = 0 }, "提交成功!");
                }
                return this.Success(new { UpLevelSuccess = false, PayMoney = r }, "提交成功!");
            }
            catch (BizException ex)
            {
                return this.FailResult(ex.data, ex.Message);
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
                return this.FailResult(null, "系统繁忙，请稍后重试!");
            }
        }


        /// <summary>
        /// 获取用户等级信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult<UserLevelInfoOutput> GetUserLevelInfo()
        {
            try
            {
                var uid = User.Claims.First(i => i.Type == "ID").Value;
                var r = userApplication.GetUserLevelInfo(int.Parse(uid));
                return this.Success(r);
            }
            catch (BizException ex)
            {
                return this.FailResult(ex.data, ex.Message);
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
                return this.FailResult(null, "系统繁忙，请稍后重试!");
            }
        }
       
    }
}
